package com.dyh.quickdemo.bizDemo.poi;

import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.*;
import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestPoi {



    @Test
    public void tes2t(){
//        4.21+5.01+5.01+3.82+3.03+9.71+7.11+9.71+27.21+29.61+5.11+5.41+4.41+9.01+7.51+7.11+4.21
//        4.21+5.01+5.01+3.82+3.03+9.71+7.11+9.71+27.2 +29.6 +5.11+5.41+4.41+9.01+7.51+7.11+4.21
        System.out.println(4.21+5.01+5.01+3.82+3.03+9.71+7.11+9.71+27.21+29.61+5.11+5.41+4.41+9.01+7.51+7.11+4.21);
    }

    @Test
    public void test() throws IOException {
        String filePath  = "D:\\work\\dyh\\quick-demo\\src\\main\\java\\com\\dyh\\quickdemo\\bizDemo\\poi\\result.xlsx";

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("下拉列表测试");

        XSSFRow head = sheet.createRow(0);
        XSSFCell cell0 = head.createCell(0);
        cell0.setCellValue("性别");

        String[] datas1 = new String[] {"男","女"};
        String[] datas2 = new String[] {"a","b","c"};
        preOption(sheet,datas1,0);

        preOption(sheet,datas2,1);
        FileOutputStream stream = new FileOutputStream(filePath);
        workbook.write(stream);
        stream.close();
    }

    private void preOption(XSSFSheet sheet, String[] datas,int col) {

        XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
        XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createExplicitListConstraint(datas);
        /*1 到100 都能下拉*/
       // CellRangeAddressList addressList =  new CellRangeAddressList(1, 100, 0, 0);
        /*指定col 列的下拉选项*/
        CellRangeAddressList addressList =  new CellRangeAddressList(1, 100, col, col);
        XSSFDataValidation validation  = (XSSFDataValidation) dvHelper.createValidation( dvConstraint, addressList);
        sheet.addValidationData(validation);
    }
}
